<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //发送Ajax请求，得到的是Promise对象
        function sendAJAX(url) {
            return new Promise((resolve, reject) => {
                //1.创建对象
                const x = new XMLHttpRequest();
                //2.初始化
                x.open('GET', url);
                //3.发送请求
                x.send()
                //4.绑定事件
                x.onreadystatechange = function() {
                    if(x.readyState === 4) {
                        if(x.status >= 200 && x.status < 300) {
                            //成功
                            resolve(x.response);
                        }else{
                            //失败
                            reject(x.status);
                        }
                    }
                }
            })
        }

        //调用then函数获取
        /* sendAJAX('http://localhost:64352/goods/GetGoodsList?token=000').then(value => {
            console.log(value);
        },reason => {}) */

        //async和awiat获取数据
        async function main() {
            // const result = await sendAJAX('http://localhost:64352/goods/GetGoodsList?token=000');
            //console.log(result);
            const huowu = await sendAJAX('https://api-hmugo-web.itheima.net/api/public/v1/categories');
            console.log(huowu);
        }
        main();
    </script>
</body>

</html>